package Dao.Impl;

import Dao.ArticleAttachmentDAO;
import entity.ArticleAttachment;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Su
 * @Date 2025/4/28 23:41
 * @Version 1.0 （版本号）
 */
public class ArticleAttachmentDAOImpl implements ArticleAttachmentDAO {
    private final Connection connection;

    public ArticleAttachmentDAOImpl(Connection connection) {
        this.connection = connection;
    }

    @Override
    public int createAttachment(ArticleAttachment attachment) throws SQLException {
        String sql = "INSERT INTO article_attachments (article_id, file_name, file_path, file_type, file_size, uploader_id) " +
                "VALUES (?, ?, ?, ?, ?, ?)";

        try (PreparedStatement stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {

            stmt.setInt(1, attachment.getArticleId());
            stmt.setString(2, attachment.getFileName());
            stmt.setString(3, attachment.getFilePath());
            stmt.setString(4, attachment.getFileType());
            stmt.setLong(5, attachment.getFileSize());
            stmt.setInt(6, attachment.getUploaderId());

            int affectedRows = stmt.executeUpdate();
            if (affectedRows == 0) {
                throw new SQLException("创建附件失败，没有行受影响");
            }

            try (ResultSet generatedKeys = stmt.getGeneratedKeys()) {
                if (generatedKeys.next()) {
                    return generatedKeys.getInt(1);
                } else {
                    throw new SQLException("创建附件失败，无法获取ID");
                }
            }
        }
    }

    @Override
    public ArrayList<ArticleAttachment> getAttachmentByArticleId(int id) throws SQLException {
        String sql = "SELECT * FROM article_attachments WHERE article_id = ?";

        try (PreparedStatement stmt = connection.prepareStatement(sql)) {

            stmt.setInt(1, id);
            ArrayList<ArticleAttachment> attachments = new ArrayList<>();
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    attachments.add(mapRowToAttachment(rs));
                }
            }
            return attachments;
        }
    }

    @Override
    public List<ArticleAttachment> getAttachmentsByArticle(int articleId) throws SQLException {
        String sql = "SELECT * FROM article_attachments WHERE article_id = ?";
        List<ArticleAttachment> attachments = new ArrayList<>();

        try (PreparedStatement stmt = connection.prepareStatement(sql)) {

            stmt.setInt(1, articleId);

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    attachments.add(mapRowToAttachment(rs));
                }
            }
        }
        return attachments;
    }

    @Override
    public boolean deleteAttachment(int id) throws SQLException {
        String sql = "DELETE FROM article_attachments WHERE id = ?";

        try (PreparedStatement stmt = connection.prepareStatement(sql)) {

            stmt.setInt(1, id);
            return stmt.executeUpdate() > 0;
        }
    }

    @Override
    public int deleteAttachmentsByArticle(int articleId) throws SQLException {
        String sql = "DELETE FROM article_attachments WHERE article_id = ?";

        try (PreparedStatement stmt = connection.prepareStatement(sql)) {

            stmt.setInt(1, articleId);
            return stmt.executeUpdate();
        }
    }

    private ArticleAttachment mapRowToAttachment(ResultSet rs) throws SQLException {
        ArticleAttachment attachment = new ArticleAttachment();
        attachment.setId(rs.getInt("id"));
        attachment.setArticleId(rs.getInt("article_id"));
        attachment.setFileName(rs.getString("file_name"));
        attachment.setFilePath(rs.getString("file_path"));
        attachment.setFileType(rs.getString("file_type"));
        attachment.setFileSize(rs.getLong("file_size"));
        attachment.setUploaderId(rs.getInt("uploader_id"));
        attachment.setUploadedAt(rs.getDate("uploaded_at"));
        return attachment;
    }
}